package com.dreamlike.demo.HttpUtil.Core.Validator;

import com.dreamlike.demo.HttpUtil.Core.HttpDefinition;

import java.util.*;

public class HttpDefinitionValidatorChain {
    private List<HttpDefinitionValidator> validators;

    public HttpDefinitionValidatorChain(HttpDefinitionValidator... httpDefinitionValidators){
        validators = new ArrayList<>(Arrays.asList(httpDefinitionValidators));
        Collections.sort(validators);
    }
    public HttpDefinitionValidatorChain addValidator(HttpDefinitionValidator httpDefinitionValidator){
        validators.add(httpDefinitionValidator);
        Collections.sort(validators);
        return this;
    }
    public HttpDefinitionValidateResult validate(HttpDefinition httpDefinition, boolean fastFail){
        HttpDefinitionValidateResult result = new HttpDefinitionValidateResult();
        for (HttpDefinitionValidator validator : validators) {
            if (!validator.validate(httpDefinition, result) && fastFail) {
                break;
            }
        }
        return result;
    }


}
